#获取每日8点新闻
import requests
from bs4 import BeautifulSoup



###拼接链接  获取到最新日期新闻的URL
url = 'https://search.cctv.com/search.php?qtext=8%E7%82%B9%E8%A7%81&page=1&type=web&sort=date&datepid=1&channel=&vtime=-1'
response = requests.get(url)
response.encoding = 'utf-8' # 或者其他正确的编码方式
content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(content, 'html.parser')
# 提取今日目标链接
link_element = soup.find(class_='tit')
link = link_element.span.attrs
print(link["lanmu1"])

# 发起HTTP请求获取网页内容
# url = 'https://news.cctv.com/2023/05/26/ARTI1vrxKsyEo0PBYrgiYtvI230526.shtml'
url = link["lanmu1"]

response = requests.get(url)
response.encoding = 'utf-8' # 或者其他正确的编码方式
content = response.text

# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(content, 'html.parser')
# print(soup.prettify())
# 根据网页结构提取相关信息
article_element = soup.find('div', class_='content_area')
# print(article_element)
if article_element is not None:
    article = article_element.text.strip()
else:
    article = 'N/A'
#打印提取的信息
# print('正文:', article)
#按空格分割消息
text = ""
segments = article.split()
for segment in segments:
    # print(segment.strip())
    text = text + segment.strip() + "\n"

lines = text.split('·')

for line in lines:
    print(line.strip())


# #发送消息到企业微信
# import requests
# import json
# url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=272de70b-d462-408e-9b15-c78281e67b84'
# headers = {'Content-Type': 'application/json'}
# data = {
#     "msgtype": "text",
#     "text": {
#         "content": article
#     }
# }
# response = requests.post(url, headers=headers, data=json.dumps(data))





